package com.uniontech.template.service.impl;

import com.uniontech.template.dao.SysFileEntityDao;
import com.uniontech.template.entity.FileEntity;
import com.uniontech.template.service.SysFileEntityService;
import com.uniontech.template.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysFileEntityServiceImpl implements SysFileEntityService {
    @Autowired
    private SysFileEntityDao fileEntityDao;

    @Override
    public FileEntity addFileEntity(FileEntity fileEntity) {
        if (fileEntity == null) {
            return fileEntity;
        }
        if (fileEntity.getCreateTime() == null) {
            fileEntity.setCreateTime(LocalDateTime.now());
        }
        if (fileEntity.getDelFlg() == null) {
            fileEntity.setDelFlg(1);
        }
        if (fileEntity.getFileId() == null) {
            fileEntity.setFileId(Tools.uuid());
        }
//        if (fileEntity.getLastScanTime() == null) {
//            fileEntity.setLastScanTime(LocalDateTime.now());
//        }
        if (fileEntityDao.add(fileEntity)) {
            return fileEntity;
        }
        return null;
    }

    @Override
    public FileEntity getFileEntityByPath(String path) {
        if (path == null || path.isEmpty()) {
            return null;
        }
        return fileEntityDao.getFileEntityByPath(path);
    }

    @Override
    public List<FileEntity> getFileList(){

        return fileEntityDao.getFileList();
    }

    @Override
    public boolean  deleteFile(Integer fileId){
        return fileEntityDao.deleteFile(fileId);
    }

    @Override
    public FileEntity getFileEntityById(Integer fileId){
        return fileEntityDao.getFileEntityById(fileId);
    }

    @Override
    public FileEntity getFileEntityByFileName(String fileName){
        return fileEntityDao.getFileEntityByFileName(fileName);
    }

    @Override
    public String getLastFileNameByName(String Name, String extName){
        return fileEntityDao.getLastFileNameByName(Name, extName);
    }
}
